<?php
/**
 * @author yuanyitian
 * @date 2024/8/13 23:18
 */

namespace api\admin;

use utils\base64_to_file;
use utils\oss;

class goods
{
    public function changeStatus($request){
        $id=(int)$request['id'];
        $status=(int)$request['status'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update goods set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    public function del($request){
        $id=(int)$request['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update goods set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

    public function edit($request){
        $id=(int)$request['id'];
        $name=$request['name'];
        $sort=$request['sort'];
        $first_price=(int)$request['first_price'];
        $price=(int)$request['price'];
        $vip_price=(int)$request['vip_price'];
        $remark=$request['remark'];
        $time=time();

        $img=$request['img'];
        $date=date('Y-m-d');
        $url=[];

        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::get("",$img[$i]["file_name"],$img[$i]["file"]);
            $res=oss::upload("goods/$date/".$file["name"],$file["path"]);
            $url[]=$res["info"]["url"];
        }

        if(empty($url)){
            $url='';
        }else{
            $url=implode(',',$url);
        }

        $con=con();
        $res=mysqli_query($con,"update goods set name='$name',sort=$sort,first_price=$first_price,price=$price,vip_price=$vip_price,remark='$remark',update_time=$time,img_url='$url' where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //商品添加
    public function add($request){
        $name=$request['name'];
        $sort=$request['sort'];
        $first_price=(int)$request['first_price'];
        $price=(int)$request['price'];
        $vip_price=(int)$request['vip_price'];
        $remark=$request['remark'];
        $time=time();
        $img=$request['img'];
        $date=date('Y-m-d');
        $url=[];
        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::get("",$img[$i]["file_name"],$img[$i]["file"]);
            $res=oss::upload("goods/$date/".$file["name"],$file["path"]);
            $url[]=$res["info"]["url"];
        }
        $url=implode(',',$url);

        $con=con();
        $res=mysqli_query($con,"insert into goods(name,sort,first_price,price,vip_price,remark,create_time,img_url) values('$name',$sort,$first_price,$price,$vip_price,'$remark',$time,'$url')");

        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //分页列表
    public function getList($requst){
        $con=con();

        $name=$requst['name'];
        $page=(int)$requst['page'];
        $size=(int)$requst['size'];
        $start=($page-1)*$size;

        $where=' where is_delete=1 ';
        if(!empty($name)){
            $where.=" and name like '%$name%'";
        }
        $sql="select * from goods $where";
        $data=mysqli_query($con,$sql." order by `id` DESC limit $start,$size");
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        $category=mysqli_query($con,"select a.*,b.id as category_id,b.name as category_name from goods_category_config a left join category b on (a.category_id=b.id and b.is_delete=1 and b.status=1) where a.is_delete=1 and a.status=1");
        $category=mysqli_fetch_all($category,true);
        $specs=mysqli_query($con,"select a.*,b.name as specs_name,IFNULL(GROUP_CONCAT(c.value),'') as specs_value from goods_specs_config a LEFT JOIN specs b on(a.specs_id=b.id and b.is_delete=1 and b.status=1) LEFT JOIN specs_value_config c on (b.id=c.specs_id and c.is_delete=1 and c.status=1) where a.is_delete=1 and a.status=1 GROUP BY a.id ");
        $specs=mysqli_fetch_all($specs,true);
        mysqli_close($con);
        for($i=0;$i<count($data);$i++){
            if(empty($data[$i]['img_url'])){
                $data[$i]['img_url']=[];
            }else{
                $data[$i]['img_url']=explode(',',$data[$i]['img_url']);
            }
            $data[$i]['category']=[];
            $data[$i]['category_ids']=[];
            $data[$i]['specs']=[];
            $data[$i]['specs_ids']=[];

            for($o=0;$o<count($category);$o++){
                if($data[$i]['id']==$category[$o]['goods_id']){
                    $data[$i]['category'][]=[
                        'id'=>$category[$o]['category_id'],
                        'name'=>$category[$o]['category_name'],
                        'disabled'=>false
                    ];
                    $data[$i]['category_ids'][]=$category[$o]['category_id'];
                }
            }

            for($o=0;$o<count($specs);$o++){
                if($data[$i]['id']==$specs[$o]['goods_id']){
                    $data[$i]['specs'][]=[
                        'id'=>$specs[$o]['specs_id'],
                        'name'=>$specs[$o]['specs_name'].'('.$specs[$o]['specs_value'].')',
                        'disabled'=>false
                    ];
                    $data[$i]['specs_ids'][]=$specs[$o]['specs_id'];
                }
            }

        }
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size)
        ]);
    }

    //获取全量商品列表
    public function getAll(){
        $con=con();
        $data=mysqli_query($con,"select * from goods where is_delete=1 and status=1");
        $data=mysqli_fetch_all($data,true);
        mysqli_close($con);
        return out_right($data);
    }

    //设置商品的分类
    public function settingCategory($data){
        $goods_id=(int)$data['id'];
        $new=$data['item'];

        $con=con();
        $time=time();
        $old=mysqli_query($con,"select category_id from goods_category_config where is_delete=1 and status=1 and goods_id=$goods_id");
        $old=mysqli_fetch_all($old,true);
        if(!empty($old)){
            $old=array_unique(array_column($old,'category_id'));
            sort($old);
        }

        $add=array_diff($new,$old);
        $delete=array_diff($old,$new);
        sort($add);
        sort($delete);



        if(!empty($add)){
            $a=[];
            for($i=0;$i<count($add);$i++){
                $id=(int)$add[$i];
                $a[]="($id,$goods_id,$time,$time)";
            }
            $add_sql=implode(',',$a);
            $res_add=mysqli_query($con,"insert into goods_category_config(category_id,goods_id,create_time,update_time) values$add_sql");
        }else{
            $res_add=true;
        }
        if(!empty($delete)){
            $delete=implode(',',$delete);
            $res_delete=mysqli_query($con,"update goods_category_config set is_delete=0,delete_time=$time where goods_id=$goods_id and category_id in ($delete)");
        }else{
            $res_delete=true;
        }
        mysqli_close($con);
        if($res_add&&$res_delete){
            return out_right('设置成功！');
        }else{
            return out_error('设置失败！');
        }

    }

    //设置商品的属性
    public function settingSpecs($data){
        $goods_id=(int)$data['id'];
        $new=$data['item'];

        $con=con();
        $time=time();
        $old=mysqli_query($con,"select specs_id from goods_specs_config where is_delete=1 and status=1 and goods_id=$goods_id");
        $old=mysqli_fetch_all($old,true);
        if(!empty($old)){
            $old=array_unique(array_column($old,'specs_id'));
            sort($old);
        }

        $add=array_diff($new,$old);
        $delete=array_diff($old,$new);
        sort($add);
        sort($delete);

        if(!empty($add)){
            $a=[];
            for($i=0;$i<count($add);$i++){
                $id=(int)$add[$i];
                $a[]="($goods_id,$id,$time,$time)";
            }
            $sql=implode(',',$a);
            $res_add=mysqli_query($con,"insert into goods_specs_config(goods_id,specs_id,create_time,update_time) values$sql");
        }else{
            $res_add=true;
        }
        if(!empty($delete)){
            $sql=implode(',',$delete);
            $res_delete=mysqli_query($con,"update goods_specs_config set is_delete=0,delete_time=$time where goods_id=$goods_id and specs_id in ($sql)");
        }else{
            $res_delete=true;
        }
        mysqli_close($con);
        if($res_add&&$res_delete){
            return out_right('设置成功！');
        }else{
            return out_error('设置失败！');
        }


    }

}